package com.edu.hre.db.sql.admin;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.edu.hre.common.utils.CommonUtils;
import com.edu.hre.db.pojo.HreLicense;
import com.edu.hre.db.service.BaseService;
import com.edu.hre.db.utils.Compare;
import com.edu.hre.struts.form.admin.LicenseForm;
import com.edu.hre.struts.form.admin.LicenseListForm;

public class LicenseDAOService implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 3394301416150421253L;
	public static final String BEAN_NAME = "licenseDAOService";
	private BaseService baseService  = null;

	public void setBaseService(BaseService baseService) {
		this.baseService = baseService;
	} 

	public ArrayList<LicenseForm> getLicenseListInfo(int limit, int offset){

		List<HreLicense> hreLicenses = baseService.getPojos(null, -1, null, new String[]{"dateCreated"}, new Boolean[]{true}, limit, offset, HreLicense.class);
		ArrayList<LicenseForm> licenses = new ArrayList<LicenseForm>();
		
		if (hreLicenses == null) {
			return licenses;
		}
		for (HreLicense hreLicense: hreLicenses) {
			
			try {

				LicenseForm license = new LicenseForm();
				license.setId(hreLicense.getId() + "");
				license.setName(CommonUtils.normalizeString(hreLicense.getName()));
				
				licenses.add(license);
			} catch (Exception e) {
				CommonUtils.println(e.getMessage());
			}
		}

		return licenses;
	}
	
	public int countLicenses(){

		int count = baseService.countByColumns(null, null, null, "id", HreLicense.class);
		return count < 0 ? 0 : count;
	}

	public void deleteLicenses(LicenseListForm form){

		ArrayList<LicenseForm> info = (ArrayList<LicenseForm>) form.getAllLicenses();
		
		for (int i = 0; i < info.size(); i++) {
			
			try {

				LicenseForm license = info.get(i);
				if (license.isSelected()) {
					baseService.deleteFirstPojo("id", Compare.EQUAL, Integer.parseInt(license.getId()), null, null, 0, HreLicense.class);
				}
			} catch (Exception e) {
				CommonUtils.println(e.getMessage());
			}
		}
	}
}
